//
//  DBJTabBarController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJTabBarController.h"
#import "DBJNavigationController.h"
#import "DBJNewFeatureView.h"
#import <Masonry.h>
#import "DBJUserAccountModel.h"
#import "DBJUserDefaultsTool.h"

@interface DBJTabBarController ()

@end

@implementation DBJTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加控制器
    [self addContorller];
    
    //检查当前的版本号是否和旧的的版本号一致 不一样添加开场页面
    NSString *versionKey = @"CFBundleVersion";
    NSString *footVersionKey = [DBJUserDefaultsTool objetForKey:versionKey];
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
    if (![footVersionKey isEqualToString:currentVersion]) {
        [self addStartView];
        //更新本地版本号
        [DBJUserDefaultsTool saveObject:currentVersion forKey:versionKey];
    }
    //将旧的版本号设置为新的版本号存储
    footVersionKey = currentVersion;
}

- (void)addContorller{
    
    //1.创建控制器
    UIViewController *home = [self getControllerWithClassName:@"DBJHomeViewController" andTitle:@"首页" andImgName:@"tab_normal_1"];
    UIViewController *find = [self getControllerWithClassName:@"DBJFindTableViewController" andTitle:@"发现" andImgName:@"tab_normal_2"];
    UIViewController *health = [self getControllerWithClassName:@"DBJHealthViewController" andTitle:@"健康" andImgName:@"tab_normal_3"];
    
    //2.为tabBar控制器添加控制器数组
    self.viewControllers = @[home,find,health];
    self.tabBar.barTintColor = [UIColor whiteColor];
}

/**
 *  根据传入的参数创建控制器
 *
 *  @param cName   控制器类名
 *  @param title   导航和tabBar的标题
 *  @param imgName tabBar的图片
 *
 *  @return 控制器
 */
- (UIViewController *)getControllerWithClassName:(NSString *)cName andTitle:(NSString *)title andImgName:(NSString *)imgName{
    
    //0.控制器
    UIViewController *controller;
    
    //1.创建导航控制器的根controler
    if ([title isEqualToString:@"首页"]) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"DBJHomeViewController" bundle:nil];
        controller = [sb instantiateInitialViewController];
    }else{
        Class class = NSClassFromString(cName);
        controller = [[class alloc]init];
    }
    
    //2.为controller设置title和tabBar的图片
    //--设置title
    controller.title = title;
    //--设置tabBarItem选中时字体的颜色 78	194	156	
    [controller.tabBarItem setTitleTextAttributes:@{
                                                    NSForegroundColorAttributeName:DBJColor
                                                    }
                                         forState:UIControlStateHighlighted];
    //--设置tabBar的图片
    UIImage *normalimg = [UIImage imageNamed:imgName];
    UIImage *selectimg = [UIImage imageNamed:[imgName stringByAppendingString:@"_selected"]];
    controller.tabBarItem.image = [normalimg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    controller.tabBarItem.selectedImage = [selectimg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self setBarItem:controller];
    
    //3.创建导航控制器
    DBJNavigationController *naController = [[DBJNavigationController alloc]initWithRootViewController:controller];
    
    //4.返回导航控制器
    return naController;
    
}

-(void)setBarItem:(UIViewController *)viewController{
    
    UIButton *LeftItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [LeftItem setBackgroundImage:[UIImage imageNamed:@"menusnew"] forState:UIControlStateNormal];
    
    
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:LeftItem];
    [LeftItem addTarget:self action:@selector(leftItemClick) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - navigationBarItem点击事件 侧滑ICSDrawerController相关事件
-(void)leftItemClick{
    [self.drawer open];
}
- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

#pragma mark - 添加开场页面
- (void)addStartView{
    
    //创建开场页面
    DBJNewFeatureView *sv = [[DBJNewFeatureView alloc]initWithFrame:self.view.bounds];
    
    //--为开场页面设置图片数据
    sv.imgs = [self getImgs];
    
    //--添加到父容器
    [self.view addSubview:sv];
    
    //--添加约束
    [sv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
}

//获取图片数据
- (NSArray *)getImgs{
    
    //图片总数
    int count = 4;
    //存储图片数据的数组
    NSMutableArray *imgs = [NSMutableArray array];
    
    for (int i = 0; i < count; i++) {
        //获取图片名
        NSString *name = [@"newFeature" stringByAppendingFormat:@"%d",i];
        //创建图片
        UIImage *img = [UIImage imageNamed:name];
        //添加到数组中
        [imgs addObject:img];
    }
    
    return imgs;
    
}

@end
